import {BaseThree} from "../base/BaseThree";
import * as THREE from 'three'
var raycaster = new THREE.Raycaster();
var mouseVector = new THREE.Vector3();
export class AddTest extends BaseThree {
  constructor( view, pos) {
    super(view,pos)
  }
  setup(pos) {
    var geometry = new THREE.BoxGeometry(1000, 1000, 1000);
    //对材质进行简单的着色
    var material = new THREE.MeshBasicMaterial({color: 0xff0000});
    //将geometry制作纹理
    var cube = new THREE.Mesh(geometry, material);
    //将位置从给定空间参考转换为内部渲染坐标系。
    let renderPos = this.toRenderCoordinates(pos);
    cube.position.set(...renderPos);
    this.cube = cube;
    console.log("物体的位置", cube.position)
    this.scene.add(cube);
    this.initRender();



    window.addEventListener( "click", this.onDocumentMouseMove.bind(this), false );
  }
  onDocumentMouseMove(event ){
    var intersects = this.getIntersects( event.layerX, event.layerY );
    console.log("结果",intersects)
  }
  getIntersects( x, y ) {
    x = ( x / window.innerWidth ) * 2 - 1;
    y = - ( y / window.innerHeight ) * 2 + 1;
    mouseVector.set( x, y, 0.5 );
    raycaster.setFromCamera( mouseVector, this.camera );
    return raycaster.intersectObject(  this.cube, true );
  }
}

